package com.example.servlet.studentServlet;

import com.example.dao.StudentDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/deleteStudent")
public class StudentDeleteServlet extends HttpServlet {
    private StudentDao studentDao = new StudentDao();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置字符编码
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

            // 获取要删除的学生ID
            String idStr = request.getParameter("id");
            int id = Integer.parseInt(idStr.trim());

            // 调用DAO删除学生
            boolean success = studentDao.deleteStudentById(id);

            if (success) {
                // 删除成功，重定向到学生列表页面
                response.sendRedirect("index.jsp");
            } else {
                // 删除失败
                request.setAttribute("error", "删除学生失败");
                request.getRequestDispatcher("studentList").forward(request, response);
            }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}
